<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ci="http://apache.org/cocoon/include/1.0" xmlns:cu="http://apache.org/cocoon/counit"
  version="1.0">
  <!-- 
    Make up the cinclude instruction: include the input xml and do a call back where needed.
    The scope of this template is the cu:<transformation> element.
  -->
  <xsl:template name="cook-cinclude">
    <xsl:param name="pipeline-to-call"/>

    <xsl:variable name="base" select="ancestor::cu:*[@base][1]/@base"/>
    <xsl:variable name="testsuite-dir" select="ancestor::cu:*[@testsuite-dir][1]/@testsuite-dir"/>
    <xsl:variable name="counit-url" select="ancestor::cu:*[@counit-url][1]/@counit-url"/>
    <cu:output>
      <ci:includexml>
        <!-- this must be a full URL and not an internal cocoon:/ pipeline, because
          otherwise the errorhandling does not work: an error in the subpipeline can
          only be caught in the main pipeline. By calling with a full URL, this can
          be circumvented.-->
        <ci:src>
          <xsl:value-of select="concat($counit-url, $pipeline-to-call)"/>
        </ci:src>
        <ci:configuration>
          <ci:parameter>
            <ci:name>method</ci:name>
            <ci:value>POST</ci:value>
          </ci:parameter>
        </ci:configuration>
        <ci:parameters>
          <ci:parameter>
            <ci:name>counit-input-method</ci:name>
            <ci:value>
              <xsl:choose>
                <xsl:when test="../@input">
                  <xsl:text>uri</xsl:text>
                </xsl:when>
                <xsl:when test="../cu:input[@preprocess='true']">
                  <xsl:text>generated</xsl:text>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:text>xml</xsl:text>
                </xsl:otherwise>
              </xsl:choose>
            </ci:value>
          </ci:parameter>
          <ci:parameter>
            <ci:name>counit-input</ci:name>
            <ci:value>
              <xsl:choose>
                <xsl:when test="../@input">
                  <xsl:value-of select="concat($base, $testsuite-dir, ../@input)"/>
                </xsl:when>
                <xsl:when test="../cu:input[@preprocess='true']">
                  <xsl:variable name="id" select="ancestor::*[@id][1]/@id"/>
                  <xsl:if test="count(../cu:input/*) != 2">
                    <xsl:message terminate="yes"><xsl:value-of select="$id"/>: a generated input document
                      should have an input element and a transformation element.</xsl:message>
                  </xsl:if>
                  <cu:generate base="{$base}" testsuite-dir="{$testsuite-dir}" counit-url="{$counit-url}"
                    id="{$id}">
                    <xsl:copy-of select="../cu:input/node()"/>
                  </cu:generate>
                </xsl:when>
                <xsl:when test="../cu:input">
                  <xsl:copy-of select="../cu:input/node()"/>
                </xsl:when>
              </xsl:choose>
            </ci:value>
          </ci:parameter>
          <!-- include local settings for this test via the caller, which should implement a template for this mode -->
          <xsl:apply-templates select="." mode="local-settings">
            <xsl:with-param name="base" select="$base"/>
            <xsl:with-param name="testsuite-dir" select="$testsuite-dir"/>
            <xsl:with-param name="counit-url" select="$counit-url"/>
          </xsl:apply-templates>
        </ci:parameters>
      </ci:includexml>
    </cu:output>
  </xsl:template>

  <!-- copy everything else -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
